﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TShock_Gift
{
    class GiftConverter : JsonConverter
    {
        public override bool CanRead => true;
        public override bool CanWrite => true;
        public override bool CanConvert(Type objectType)
        {
            return typeof(Gift) == objectType;
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Gift gift = new Gift();
            gift.name = reader.ReadAsString();
            gift.id = (int)reader.ReadAsInt32();
            reader.Read();
            reader.Read();
            gift.number[0] = Convert.ToInt32(reader.Value);
            reader.Read();
            gift.number[1] = Convert.ToInt32(reader.Value);
            reader.Read();

            gift.proportion = (int)reader.ReadAsInt32();
            reader.Read();
            return gift;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            Gift gift = value as Gift;
            writer.WriteStartArray();
            writer.WriteValue(gift.name);
            writer.WriteValue(gift.id);
            writer.WriteStartArray();
            for (int i = 0; i < gift.number.Length; i++)
            {
                writer.WriteValue(gift.number[i]);
            }
            writer.WriteEndArray();
            writer.WriteValue(gift.proportion);
            writer.WriteEndArray();
        }
    }
    [JsonConverter(typeof(GiftConverter))]
    public class Gift
    {
        public string name = "";
        public int id = 0;
        public int[] number = new int[2];
        public int proportion = 0;
    }

    public class Config
    {
        public bool Enable = true;
        public int TimeUnit = 60 * 1000;
        public Dictionary<int,string> PointOfTime = new Dictionary<int,string>();
        public string DefaultMessage = "你已进行{0}分钟游戏";
        public List<Gift> Gifts = new List<Gift>();

        [JsonIgnore]
        public int denominator = 0;

        public static string path = "./tshock/gift.json";
        private Config()
        {

        }
        public static Config LoadConfig()
        {
            if (!File.Exists(path))
            {
                Config config = new Config();
                Gift demo = new Gift()
                {
                    name = "[物品注释,ID,[物品数量最小值,物品数量最小值],比重]",
                    id = 0,
                    number = new int[] { 0, 1 },
                    proportion = 0,
                };
                config.Gifts.Add(demo);
                config.PointOfTime.Add(30, "你已在线30分钟  ");
                File.WriteAllText(path, JsonConvert.SerializeObject(config));
                return config;
            }
            else
            {
                Config config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(path));
                for (int i = 0; i < config.Gifts.Count; i++)
                {
                    config.denominator += config.Gifts[i].proportion;
                }
                if (config == null)
                {
                    throw new FormatException("配置文件'gift.json'读取出错！");
                }
                return config;
            }
        }
    }
}
